#!/usr/bin/perl
#
# Opcounts - look at the log of events for a month
# count the number of ops.
#
# Param: table Fromdate
#
use Counter;
use Getopt::Long;

our $periods = 1;
our $store = 0;
our $interval = "month";
our $opcounts;

GetOptions(
	   "periods=i" => \$periods,
	   "store" => \$store,
	   "interval=s" => \$interval,
	   );


my $table = shift;
my $fromdate = shift;
die "Not a month: $fromdate\n" if $fromdate !~ /^\d\d\d\d-\d\d/;
$fromdate .= "-01"; # add first-of-month to make MySQL get it right

print "Stats for $periods $interval(s) from $fromdate\n";
my $stats = Counter::openfile("statistics");
if ($store) {
    $opcounts = Counter::openfile("opcounts");
}
my @oplist = $stats->selectany("distinct operation from opcounts");
for my $op (@oplist) {
    $toseelist{$op->{operation}} = "no";
}


while ($periods) {
    stats($fromdate);
    my @x = $stats->selectany("date_add(?, interval 1 $interval) as d", $fromdate);
    $fromdate = $x[0]->{d};
    -- $periods;
}


sub stats {

    print "# Stats for $fromdate\n";
    @counts = $stats->selectany("operation, count(*) as c from f_log 
      where date >= ? and date < date_add(?, interval 1 $interval)
      and tablename = ? group by operation",
				$fromdate, $fromdate, $table);
    my %seenlist = %toseelist;
    for $count (@counts) {
	$seenlist{$count->{operation}} = "yes";
	print $count->{operation}, "\t", $count->{c}, "\n";
	$op{$count->{operation}} = $count->{c};
	if ($store) {
	    my $oprec = $opcounts->newnum();
	    $oprec->{name} = $table;
	    $oprec->{operation} = $count->{operation};
	    $oprec->{qualifier} = '';
	    $oprec->{date} = $fromdate;
	    $oprec->{period} = $interval;
	    $oprec->{count} = $count->{c};
	    if ($count->{operation} eq 'new') {
		$oprec->{impact} = $count->{c};
	    } elsif ($count->{operation} eq 'delete') {
		$oprec->{impact} = -($count->{c});
	    } else {
		$oprec->{impact} = 0;
	    }
	    $oprec->store();
	}
    }
    for $op (keys(%seenlist)) {
	if ($seenlist{$op} eq "no") {
	    print "$op\tnone\n";
	    if ($store) {
		my $oprec = $opcounts->newnum();
		$oprec->{name} = $table;
		$oprec->{operation} = $op;
		$oprec->{qualifier} = '';
		$oprec->{date} = $fromdate;
		$oprec->{period} = $interval;
		$oprec->{count} = 0;
		$oprec->{impact} = 0;
		$oprec->store();
	    }
	}
    }
	    
# check if we have begin-end statistics
    my @fromstat = $stats->select("date = ? and name = ? and qualifier = ''",
				  $fromdate, $table);
    my @tostat = $stats->select("date = date_add(?, interval 1 $interval) and name = ? and qualifier = ''",
				$fromdate, $table);
    if (@fromstat == 1 && @tostat == 1) {
	print "We have stats\n";
	my $fromcount = $fromstat[0]->{value};
	my $tocount = $tostat[0]->{value};
	
	my $fudge = $tocount - ($fromcount + $op{new} - $op{delete});
	print "reborn\t$fudge\n";
	if ($store) {
	    my $oprec = $opcounts->newnum();
	    $oprec->{name} = $table;
	    $oprec->{operation} = "fudge";
	    $oprec->{qualifier} = '';
	    $oprec->{date} = $fromdate;
	    $oprec->{period} = $interval;
	    $oprec->{count} = $fudge;
	    $oprec->{impact} = $fudge;
	    $oprec->store();
	}
    }
}
